const $api = require('./../../utils/request').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    projectTypeList: [{ projectTypeName: '全部', id: '' }],
    allData: [],
    projectList: [],
    choseData: [],
    goodsNum: 0,
    roomId:'',
    roomCode:"",
    orderId:null
  },


  onChange(event) {
    var id = event.currentTarget.dataset.id
    let arr = this.data.allData;
    arr.forEach(el => {
      if (el.id == id) {
        el.num = event.detail
      }
    })
    this.setData({
      allData: arr
    })
    var choseData = [];
    var goodsNum = 0;
    setTimeout(() => {
      this.data.allData.forEach(element => {
        if (element.num != 0) {
          choseData.push(element);
          goodsNum = goodsNum + element.num
          this.setData({
            choseData: choseData,
            goodsNum: goodsNum
          })
        }
      })
    }, 300);
  },

  async init(roomId) {
    const { page } = await $api.getProjectType();

   
    const goods = await $api.getGoods({ roomId });

    var goodsList = [];
    if (goods.data.length > 0) {
      goods.data.forEach(e => {
        goodsList.push(e);
      })
    }

  

    if (page.list.length > 0) {
      let listdata = [];
      page.list.forEach(datas => {
        // if (datas.isJiShiProjectType == 0) {
        //   listdata.push(datas)
        // }
        listdata.push(datas)
      })
      let arr = this.data.projectTypeList
      var newArr = arr.concat(listdata)
      this.setData({
        projectTypeList: newArr
      })
      const data = await $api.getProjectList({page:1,limit:9999});
      var projectdatas = [];
      data.page.list.forEach(e => {
        // if (e.needJiShiStatusChk == 0) {
        //   e.num = 0;
        //   projectdatas.push(e)
        // }
                  e.num = 0;
          projectdatas.push(e)
      })


      if (goodsList.length > 0) {
        projectdatas.forEach(e => {
          e.isBuy = 0;
          goodsList.forEach(i => {
            if (e.id == i.projectId) {
              e.isBuy = e.isBuy + i.count 
            }
          })
        });
        this.setData({
          projectList: projectdatas,
          allData: projectdatas
        })
      } else {
        this.setData({
          projectList: projectdatas,
          allData: projectdatas
        })
      }
    }
  },

  async getProjecetList(id) {
    const data = await $api.getProjectList({ tprojectTypeIdFk: id })
    this.setData({
      projectList: data.page.list
    })
  },


  chosePro(e) {
    var arr = [];
    this.setData({
      projectList: [],
    })
 
    if (e.currentTarget.dataset.id) {
      this.data.allData.forEach(element => {
        if (e.currentTarget.dataset.id == element.tprojectTypeIdFk) {
          arr.push(element);
        }
      })
      this.setData({
        projectList: arr,
      })
    } else {
      this.setData({
        projectList: this.data.allData
      })
    }
    this.setData({
      active: e.currentTarget.dataset.index
    })

  },

  onLoad: function (options) {
    let roomId = Number(options.roomId);
    let roomCode = options.roomCode
    this.setData({
      roomId:roomId,
      goodsNum:0,
      roomCode:roomCode
    })
    wx.setNavigationBarTitle({
      title: `【 ${roomCode} 】选择项目`
    })
    this.init(roomId);
  },



  xiaofeiruzhang() {
    var arr = [];
    var needJishiArr = [];
    this.data.projectList.forEach(e=>{
      if(e.num != 0 && e.needJiShiStatusChk == 0){
        let obj = {};
        obj.id = e.id;
        obj.count = e.num;
        arr.push(obj)
      }
    })

   

    this.data.choseData.forEach((e,index)=>{
      if(e.needJiShiStatusChk == 1){

        let obj = {
          cost:e.projcetDanJia, 
          roomCode:this.data.roomCode,
          count:1,
          jishiid:wx.getStorageSync('userId'),
          roomId:this.data.roomId, 
          projectId:e.id,
          projectName:e.projectName,
          shougongdanId: ((new Date()).valueOf())+index,
        };
        needJishiArr.push(obj)
      }
    })

    needJishiArr.forEach(async e=>{
      await $api.addproject(e);
    });


    let params = {
        roomId:this.data.roomId,
        jishiId:wx.getStorageSync('userId'),
        list:arr
    };
    $api.addFeiZyGoods(params).then(res=>{
      if( res.code ==  '200' ){
        if( res.data == null ){
          wx.showToast({
            title: '入账成功',
            duration:1500,
            icon:'none'
          })
          setTimeout(() => {
            wx.reLaunch({
              url:'../home/home'
            })
          }, 1500);
        }else{
          wx.showToast({
            title: res.msg,
            duration:1500,
            icon:'none'
          })
          setTimeout(() => {
            wx.reLaunch({
              url:'../home/home'
            })
          }, 1500);

        }
      }else{
        wx.showToast({
          title: res.msg,
          duration:1500,
          icon:'none'
        })


      }
    }).catch(err=>{
      if( err.code == '500'){
        if( needJishiArr.length != 0 ){
          wx.showToast({
            title: '添加成功',
            duration:1500,
            icon:'none'
          })
          setTimeout(() => {
            wx.reLaunch({
              url:'../home/home'
            })
          }, 1500);
        }else{
          wx.reLaunch({
            url:'../home/home'
          })
        }


        }else{
          wx.showToast({
            title: err.msg,
            duration:1500,
            icon:'none'
          })
        }



    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})